/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.display.TransitionImageDisplayer;
import me.panpf.sketch.process.GaussianBlurImageProcessor;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.util.ResUtils;
import me.panpf.sketch.sample.widget.SampleImageView;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * GaussianBlurImageProcessorTestPageView
 */
public class GaussianBlurImageProcessorTestPageView extends AbstractPageView {

    private int mProgress = 15;
    private SketchImageView mImage_gaussianBlurFragment;

    public GaussianBlurImageProcessorTestPageView(Context Context, String name) {
        super(Context, name);
    }

    @Override
    public void initView() {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();

        setRootView(ResUtils.parseLayout(context, ResourceTable.Layout_gaussian_blur_image_processor_page_view));

        mImage_gaussianBlurFragment = (SketchImageView) getRootView().findComponentById(ResourceTable.Id_image_gaussianBlurFragment);
        mImage_gaussianBlurFragment.getOptions().setMaxSize(attributes.width/4,attributes.height/4);

        Text text = (Text) getRootView().findComponentById(ResourceTable.Id_text_gaussianBlurFragment);

        Slider slider = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_gaussianBlurFragment);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b1) {
                text.setText(i1 + "/100");
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                mProgress = slider.getProgress();
                apply();
            }
        });

        slider.setMaxValue(100);
        mProgress = 15;
        slider.setProgressValue(mProgress);
        apply();
    }

    private void apply() {
        mImage_gaussianBlurFragment.getOptions().setProcessor(GaussianBlurImageProcessor.makeRadius(mProgress));
        Sketch.with(getContext()).displayFromResource(ResourceTable.Media_mei_nv, mImage_gaussianBlurFragment).commit();
    }
}
